<template>
  <div class="postlist">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top: 20px">
      <!-- 表格 -->
      <el-table :data="postlist" border style="width: 100%">
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="title" label="标题" width="680">
        </el-table-column>
        <el-table-column label="类型" width="180">
          <template slot-scope="scope">
            {{ scope.row.type == 1 ? "文章" : "视频" }}
          </template>
        </el-table-column>
        <el-table-column prop="user.nickname" label="作者" width="180">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!--  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total - 0"
        style="margin-top: 10px"
      >
      </el-pagination>
    </el-card>
    <!--  -->
  </div>
</template>

<script>
import { getPostList } from "@/apis/post.js";
export default {
  data() {
    return {
      postlist: [],
      pageIndex: 1,
      pageSize: 2,
      total: "",
    };
  },
  methods: {
    del(scope) {
      console.log(scope);
    },
    //每页显示的数量列表
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //重置全局pageSize
      this.pageSize = val;
      // 重置pageSize后重新获取数据
      this.init();
    },
    // 当前页码
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageIndex = val;
      this.init();
    },
    //获取文章数据
    async init() {
      let res = await getPostList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      });
      console.log(res);
      this.postlist = res.data.data;
      this.total = res.data.total;
    },
  },

  async mounted() {
    this.init();
  },
};
</script>

<style lang="less" scoped>
</style>